package com.example.spinnertest.tools;



import com.example.spinnertest.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ViewGroupTest extends ViewGroup{

	public ViewGroupTest(Context context) {
		super(context);
	}
	public ViewGroupTest(Context context,AttributeSet attrs ) {
		super(context,attrs);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int mHeightTotal = 0;
		for (int i = 0; i < getChildCount(); i++) {
			View childView = getChildAt(i);
			int measuredWidth=childView.getMeasuredWidth();
			int measureHeight = childView.getMeasuredHeight();
			childView.layout(l, mHeightTotal, measuredWidth, mHeightTotal+measureHeight);
			mHeightTotal+=measureHeight;
		}

	}

	//widthMeasureSpec,heightMeasureSpec可以获得的空间以及空间描述的元数据
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int measuredWidth = measureWidth(widthMeasureSpec); 
		int measuredHeight = measureHeight(heightMeasureSpec); 
		setMeasuredDimension(measuredWidth, measuredHeight); 
		for(int i= 0;i<getChildCount();i++){ 
			View v = getChildAt(i); 

			
		
			//            LayoutParams params = v.getLayoutParams(); 
			//            if(params.width > 0){ 
			//                widthSpec = MeasureSpec.makeMeasureSpec(params.width, MeasureSpec.EXACTLY); 
			//            }else if (params.width == -1) { 
			//                widthSpec = MeasureSpec.makeMeasureSpec(measureWidth, MeasureSpec.EXACTLY); 
			//            } else if (params.width == -2) { 
			//                widthSpec = MeasureSpec.makeMeasureSpec(measureWidth, MeasureSpec.AT_MOST); 
			//            } 
			//
			//            if(params.height > 0){ 
			//                heightSpec = MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY); 
			//            }else if (params.height == -1) { 
			//                heightSpec = MeasureSpec.makeMeasureSpec(measureHeight, MeasureSpec.EXACTLY); 
			//            } else if (params.height == -2) { 
			//                heightSpec = MeasureSpec.makeMeasureSpec(measureWidth, MeasureSpec.AT_MOST); 
			//            } 
			//使用父容器的空间规格说明,shiyong译解后空间作为子view空间
			v.measure(measuredWidth, measuredHeight); 

		} 
	}

	
	public int measureWidth(int measureWidth)
	{
		int result =0;
		int flag = MeasureSpec.getMode(measureWidth);
		switch (flag) {
		case MeasureSpec.AT_MOST:
		case MeasureSpec.EXACTLY:
			result = MeasureSpec.getSize(measureWidth);
		default:
			break;
		}
		return result;
	}

	public int measureHeight(int measureHeight)
	{
		int result =0;
		int flag = MeasureSpec.getMode(measureHeight);
		switch (flag) {
		case MeasureSpec.AT_MOST:
		case MeasureSpec.EXACTLY:
			result = MeasureSpec.getSize(measureHeight);
		default:
			break;
		}

		return result;
	}
}
